package com.fly.shiroauth.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {


    private Set<String> getRolesByUsername(String username) {
        //  查询db后封装数据
        Set<String> roles = new HashSet<>();
        roles.add("admin");
        roles.add("user");
        return roles;
    }

    private Set<String> getPermissionsByUsername(String username) {
        //  查询db后封装数据
        Set<String> permissions = new HashSet<>();
        permissions.add("user:delete");
        permissions.add("user:add");
        return permissions;
    }

    /*
        授权:
        根据用户名 查询db,
            返回roles 和 permissions
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        String username = (String) principalCollection.getPrimaryPrincipal();
        Set<String> roles = this.getRolesByUsername(username);
        authorizationInfo.setRoles(roles);

        Set<String> permissions = this.getPermissionsByUsername(username);
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    /*
        鉴权:
        1. 根据认证信息获取用户名
        2. 通过用户名获取密码
        3. 使用 用户名 密码 资源名 构造 鉴权对象 并添加盐值
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String username = (String) authenticationToken.getPrincipal();
        String password = this.getPasswordByUsername(username);
        if (password != null) {
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, "myRealm");
            authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("abcd"));
            return authenticationInfo;
        }
        return null;
    }

    private String getPasswordByUsername(String username) {
        return userDbMap.get(username);
    }

    // 模拟数据库 <username, password>
    Map<String, String> userDbMap = new HashMap<>();

    {
//        userDbMap.put("fly", "123456");
        // 加密
        userDbMap.put("fly", "da3177cbd9f064004b6a0d59a3a484bb");
        super.setName("myRealm");
    }

    public static void main(String[] args) {
        Md5Hash md5Hash = new Md5Hash("123456", "abcd");
        System.out.println(md5Hash.toString());
    }
}
